package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.landmarkDetailQueryObject;
import cn.wolfcode.trip.base.service.ILandmarkDetaiService;
import cn.wolfcode.trip.base.service.ILmTicketCatalogBigService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("headlineCommend")
public class HeadlineCommendController {
    @Autowired
    private ILandmarkDetaiService landmarkDetaiService;
    @Autowired
    private ILmTicketCatalogBigService lmTicketCatalogBigService;

    //推荐头条管理
    @RequestMapping("list")
    public String list(Model model , @ModelAttribute("qo")landmarkDetailQueryObject qo){
        List<LandmarkDetai> result = landmarkDetaiService.getLmDetailByState(1);
        List<LmTicketCatalogBig> lcDetails = lmTicketCatalogBigService.getLmTicketCatalogBigByState(2);
        model.addAttribute("result",result);
        model.addAttribute("lcDetails",lcDetails);
        return "headlineCommend/list";
    }

    @RequestMapping("/getContentById")
    @ResponseBody
    public Object getContentById(Long id){
        LandmarkContent content= landmarkDetaiService.getContentById(id);
        return content;
    }
    @RequestMapping("/getCatalogBigId")
    @ResponseBody
    public Object getCatalogBigId(Long cbId){
        LtCatalogDetail ltCatalogDetail = landmarkDetaiService.getCatalogBigId(cbId);
        return ltCatalogDetail;
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(LandmarkDetai landmarkDetai, MultipartFile file){
        JSONResult result = new JSONResult();
        Long bcId = landmarkDetai.getLcdId();
        LtCatalogDetail ltc = landmarkDetaiService.getIdByCatalogBigId(bcId);
        landmarkDetai.setLcdId(ltc.getId());
        try {
            if(file != null && file.getSize() > 0){
                // 将图片上传到图片服务器
                String imgUrl = UploadUtil.upload(file, UploadUtil.BASE_PATH + "upload");
                landmarkDetai.setCoverURL(imgUrl);
            }
            landmarkDetaiService.saveOrUpdate(landmarkDetai);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
